﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XDYRestaurant
{
    public partial class TableInfo : Form
    {
        private Context.Context ctx = new Context.Context();
        private static TableInfo _TableInfo;
        private object int64;

        public TableInfo()
        {
            InitializeComponent();
        }
        public static TableInfo Create()
        {
            if (_TableInfo != null)
            {
                _TableInfo.Close();
            }
            _TableInfo = new TableInfo();
            return _TableInfo;
        }
        private void dgvTableInfoList_CellDoubleClick(object sender, EventArgs e)
        {
            try
            {
                txtId.Text = dgvTableInfoList.SelectedRows[0].Cells[0].Value.ToString();
                if(dgvTableInfoList.SelectedRows[0].Cells[1].Value == null)
                {
                    txtCustomerPhone.Text = "";
                }
                else
                {
                    txtCustomerPhone.Text = dgvTableInfoList.SelectedRows[0].Cells[1].Value.ToString();
                }
                if(dgvTableInfoList.SelectedRows[0].Cells[2].Value.ToString() == "0")
                {
                    rbTableFree.Checked = true;
                }
                else
                {
                    rbTableBusy.Checked = true;
                }
            }
            catch
            {
                //兼容性：如果选中非法就什么也不干
                return;
            }
        }

        private void btnShowAllInfo_Click(object sender, EventArgs e)
        {
            var tmp = -1;
            if (cmbSearchTableFree.Text == "使用中") tmp = 1;
            if (cmbSearchTableFree.Text == "空闲") tmp = 0;
            var list = ctx.Tables.Where(s => true).ToList(); ;
            if(tmp == -1)
            {
                list = ctx.Tables.Where(s => true).ToList();
            }
            else
            {
                list = ctx.Tables.Where(s => s.TableStatic == tmp).ToList();
            }
            dgvTableInfoList.DataSource = list;
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtId.Text = "";
            txtCustomerPhone.Text = "";
            rbTableFree.Checked = true;
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(txtId.Text.Trim());
                DialogResult res = MessageBox.Show("确认要删除吗?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (res == DialogResult.OK)
                {
                    var table = ctx.Tables.FirstOrDefault(u => u.TableId == id);
                    ctx.Tables.Remove(table);
                    ctx.SaveChanges();
                    MessageBox.Show("删除成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cmbSearchTableFree.SelectedIndex = 0;
                    var list = ctx.Tables.Where(s => true).ToList();
                    dgvTableInfoList.DataSource = list;
                }
            }
            catch
            {
                MessageBox.Show("该餐桌上还有顾客正在用餐或选中后未双击!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dgvTableInfoList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                e.Value = Convert.ToBoolean(e.Value) ? "×" : "√";
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int id,f;
            Int64 ff;
            string phone = txtCustomerPhone.Text.Trim().ToString();
            byte t;
            if (rbTableFree.Checked == true)
            {
                t = 0;
            }
            else
            {
                t = 1;
            }
            if (!int.TryParse(txtId.Text.Trim(), out f))
            {
                MessageBox.Show("请输入正确的餐桌号！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            id = Convert.ToInt32(txtId.Text.Trim());
            if (id <= 0)
            {
                MessageBox.Show("请输入正确的餐桌号！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Models.Table table = new Models.Table();
            Models.Customer customer = new Models.Customer();
            customer = ctx.Customers.FirstOrDefault(s => s.CustomerPhone == phone);
            if (rbTableBusy.Checked == true)
            {
                if (phone.Length != 11)
                {
                    MessageBox.Show("请输入11位顾客手机号！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (!Int64.TryParse(phone, out ff))
                {
                    MessageBox.Show("请输入正确的顾客手机号！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                table.CustomerPhone = phone;

                if (id <= 0)
                {
                    MessageBox.Show("请输入正确的餐桌号！", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (customer == null)
                {
                    MessageBox.Show("该顾客不存在!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (customer.TableId != null)
                {
                    MessageBox.Show("该顾客正在其他地方就餐!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                customer.TableId = id;
            }
            else
            {
                table.CustomerPhone = null;
            }

            table.TableId = id;
            table.TableStatic = t;
            
            
            if (ctx.Tables.FirstOrDefault(s => s.TableId == id) == null)
            {
                ctx.Tables.Add(table);
                ctx.SaveChanges();
                MessageBox.Show("添加成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cmbSearchTableFree.SelectedIndex = 0;
                var list = ctx.Tables.Where(s => true).ToList(); 
                dgvTableInfoList.DataSource = list;
            }
            else
            {
                try
                {
                    if (ctx.Tables.FirstOrDefault(s => s.TableId == id).TableStatic == 1)
                    {
                        MessageBox.Show("该餐桌有顾客正在就餐!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    DialogResult res = MessageBox.Show("确认要修改吗?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (res != DialogResult.OK) return;
                    var table2 = ctx.Tables.FirstOrDefault(s => s.TableId == id);
                    table2.CustomerPhone = phone;
                    table2.TableStatic = t;
                    ctx.SaveChanges();
                    MessageBox.Show("修改成功!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cmbSearchTableFree.SelectedIndex = 0;
                    var list = ctx.Tables.Where(s => true).ToList();
                    dgvTableInfoList.DataSource = list;
                }
                catch
                {
                    MessageBox.Show("添加或修改失败!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
